package priv.abadstring.study.haoke.webapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.abadstring.study.haoke.dubbo.dto.PageInfo;
import priv.abadstring.study.haoke.dubbo.entity.Ad;
import priv.abadstring.study.haoke.webapi.manage.AdManage;
import priv.abadstring.study.haoke.webapi.vo.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * AdController
 *
 * @author aBadString
 * @date 2021-06-18 19:37:12
 */
@RestController
@RequestMapping("ad")
// 跨域支持
@CrossOrigin
public class AdController {

    @Autowired
    private AdManage adManage;

    @GetMapping
    public WebResult list() {
        PageInfo<Ad> list = this.adManage.list(1, 1, 3);

        ArrayList<HashMap<String, String>> original = list.getRecords().stream()
            .map(
                (ad) -> {
                    HashMap<String, String> map = new HashMap<>(1);
                    map.put("original", ad.getUrl());
                    return map;
                }
        ).collect(Collectors.toCollection(() -> new ArrayList<>(list.getPageSize())));

        return WebResult.ok(original);
    }
}
